package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
//此类实现景点的查找与排序功能
public class ScenicsFindAndSort extends JFrame {
	private JPanel scenicsFindAndSortPanel;

	public ScenicsFindAndSort() {
		super("景点的查找与排序");
		setSize(1000, 700);
		setLocation(400, 200);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
	}
	//实例化一个scenic对象
	function.Scenic scenic = new function.Scenic();

	public void scenicsFindAndSortPanel() {
		scenicsFindAndSortPanel = new JPanel();
		scenicsFindAndSortPanel.setLayout(null);

		JLabel mentionLabel = new JLabel("请选择排序方式");
		mentionLabel.setBounds(50, 150, 250, 30);
		mentionLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(mentionLabel);
		
		ButtonGroup group0 = new ButtonGroup();
		JRadioButton rb1 = new JRadioButton("景点欢迎度", true);
		rb1.setBounds(50, 190, 200, 30);
		rb1.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(rb1);
		group0.add(rb1);
		
		JRadioButton rb2 = new JRadioButton("景点的岔路数", true);
		rb2.setBounds(50, 230, 200, 30);
		rb2.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(rb2);
		group0.add(rb2);
	
		JLabel mentionLabel1 = new JLabel("请选择升序或降序");
		mentionLabel1.setBounds(50, 300, 250, 30);
		mentionLabel1.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(mentionLabel1);
		
		ButtonGroup group1 = new ButtonGroup();
		JRadioButton rb3 = new JRadioButton("升序", true);
		rb3.setBounds(50, 340, 150, 30);
		rb3.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(rb3);
		group1.add(rb3);
		
		JRadioButton rb4 = new JRadioButton("降序", true);
		rb4.setBounds(50, 380, 150, 30);
		rb4.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(rb4);
		group1.add(rb4);
		//informationLabel显示输出的查询内容或排序结果
		JLabel informationLabel = new JLabel("");
		informationLabel.setBounds(430, 80, 500, 500);
		informationLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(informationLabel);
		
		JTextField inquireText = new JTextField(30);
		inquireText.setBounds(250, 50, 200, 40);
		inquireText.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(inquireText);
		//此处为关键字查询
		JButton button = new JButton("查询");
		button.setBounds(480, 50, 120, 40);
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = inquireText.getText();
				//调用scenic类的fuzzyQuery方法实现关键字查询
				String information = scenic.fuzzyQuery(text);
				//将查询结果显示出来
				informationLabel.setText(information);
			}
		});
		
		JButton button2 = new JButton("排序");
		button2.setBounds(170, 430, 100, 40);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		scenicsFindAndSortPanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//用户选择排序方式，可按景点欢迎度或景点的岔路数排序
				String way = null;
//				order用于选择按升序还是降序排序
				String order=null;
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						way = btn.getText();
						break;
					}
				}
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns2 = group1.getElements();
				while (radioBtns2.hasMoreElements()) {
					AbstractButton btn = radioBtns2.nextElement();
					if (btn.isSelected()) {
						order = btn.getText();
						break;
					}
				}

				String information=null;
				if(way.equals("景点欢迎度")) {
			//若用户选择按景点欢迎度排序，则调用scenic类的sortByPopularity方法
					information=scenic.sortByPopularity(order);
				} else {
//			否则调用scenic类的sortByPathNumber方法按岔路数排序
					information=scenic.sortByPathNumber(order);
				}
//				在界面打印出排序结果
				informationLabel.setText(information);
			}
		});

		scenicsFindAndSortPanel.setVisible(true);
		add(scenicsFindAndSortPanel);
		setVisible(true);
	}
}
